package com.jackrain.nea.ad.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.api.AdactionDeleteCmd;
import com.jackrain.nea.ad.api.AdactionSaveCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import com.jackrain.nea.web.security.Security4Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(value = "AD_MONIOTR",description = "动作定义 bigzhi")
@RestController
public class AdactionCtrl {
    @Reference(version = "1.0", group = "ad")
    private AdactionSaveCmd adactionSaveCmd;
    @Reference(version = "1.0", group = "ad")
    private AdactionDeleteCmd adactionDeleteCmd;

    @ApiOperation(value = "带有QuerySession的 新增、更新 测试")
    @RequestMapping(path = "api/action/save", method = RequestMethod.POST)
    public JSONObject saveSession(HttpServletRequest request,
                                  @RequestParam(value = "param", required = true) String param) throws Exception {

        //暂时使用root账号
        User user = (User) Security4Utils.getUser("root");
        QuerySessionImpl querySession = new QuerySessionImpl(user);
        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
        event.put("param", JSON.parseObject(param));
        querySession.setEvent(event);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdactionSaveCmd", "ad", "1.0");
        ValueHolder result = ((AdactionSaveCmd)o).execute(querySession);

        return result.toJSONObject();
    }

    @ApiOperation(value = "带有QuerySession的 删除 测试")
    @RequestMapping(path = "/api/action/delete", method = RequestMethod.POST)
    public JSONObject delSession(HttpServletRequest request,
                                 @RequestParam(value = "param", required = true) String param) throws Exception {

        //暂时使用root账号
        User user = (User) Security4Utils.getUser("root");
        QuerySessionImpl querySession = new QuerySessionImpl(user);
        DefaultWebEvent event = new DefaultWebEvent("test", request, false);
        event.put("param", JSON.parseObject(param));
        querySession.setEvent(event);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdactionDeleteCmd", "ad", "1.0");
        ValueHolder result = ((AdactionDeleteCmd)o).execute(querySession);

        return result.toJSONObject();
    }
}
